package com.example.kechengsheji;


import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;


public class denglu extends zhuce {
    private EditText accountEdit;
    private EditText passwordEdit;
    private Button login;
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    private CheckBox rememberpass;
    private Button registe;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checkbox);

        accountEdit = (EditText) findViewById(R.id.account);
        passwordEdit = (EditText) findViewById(R.id.passeord);
        login = (Button) findViewById(R.id.login);
        rememberpass = (CheckBox) findViewById(R.id.remember_password);
        registe = (Button) findViewById(R.id.registe);
        registe.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(denglu.this, zhuce.class);
                startActivity(intent);
            }
        });
        pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        boolean isRemember = pref.getBoolean("remember_password", true);
        if(isRemember){
            //将账号和密码都设置在文本框内
            String account = pref.getString("account", "");
            String password = pref.getString("password", "");
            accountEdit.setText(account);
            passwordEdit.setText(password);
            rememberpass.setChecked(true);
        }
        login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String account = accountEdit.getText().toString();
                String password = passwordEdit.getText().toString();
                SharedPreferences pre = getSharedPreferences("data", MODE_PRIVATE);

                if(account.equals(pre.getString("name", ""))&&password.equals(pre.getString("password", ""))){
                    editor = pref.edit();
                    if (rememberpass.isChecked()) {
                        editor.putBoolean("remember_password", true);
                    }
                    else{
                        editor.clear();
                    }
                    editor.commit();
                    Intent intent = new Intent(denglu.this, choose.class);
                    startActivity(intent);
                    finish();
                }
                else {
                    Toast.makeText(denglu.this, "用户名或密码不对", Toast.LENGTH_LONG).show();
                }
            }
        });
    }


}